//
//  SEGOldPGCListViewLayout.m
//  SEGPlatform
//
//  Created by SEGI on 2020/12/3.
//

#import "SEGOldPGCListViewLayout.h"
#import <SEGPlatform/SEGPlatformManager.h>

/**************************SEGOldPGCListViewLayout************************/
@implementation SEGOldPGCListViewLayout
- (instancetype)initWithData:(SEGOldPGCListViewModel *)data type:(SEGEnumOldPGCListViewType)type{
    if (self == [super init]) {
        _data = data;
        _type = type;
    }
    [self segOldPGCListViewLayout];
    return self;
}

-(void)segOldPGCListViewLayout{
    //高度固定
    _totalHeight = _data.widgetHeight;
    
    //高度不固定
    _totalHeight = 0;
    _contentLabelHeight = 0;
    _contentLabelTextLayout = nil;
    _praiseLabelHeight = 0;
    _praiseLabelTextLayout = nil;
    _commentLabelHeight = 0;
    _commentLabelTextLayout = nil;
    _timeDescLabelHeight = 0;
    _timeDescLabelTextLayout = nil;
    
    if (!_data) return;
    if (!_data.pictorialData) return;
    
    [self layout];
    
}

//数据处理
-(void)layout{
    
    [self segpraiseLabelWithContent:_data.pictorialData.praiseTotal fontSize:13 textColor:kColor_3];
    [self segcommentLabelWithContent:_data.pictorialData.commentTotal fontSize:13 textColor:kColor_3];
    [self segtimeDescLabelWithContent:_data.pictorialData.createTime fontSize:13 textColor:kColor_3];
    
    switch (self.type) {
        case SEGEnumOldPGCListViewType_OnlyWords:
        {
            _contentLabelWidth = self.data.widgetWidth - self.data.marginOnlyWordsContentInset.left - self.data.marginOnlyWordsContentInset.right;
            [self segcontentLabelWithContent:_data.pictorialData.pictorialTitle contentWidth:_contentLabelWidth  fontSize:16 textColor:kColor_1];
            
            _totalHeight += _contentLabelHeight + _data.marginOnlyWordsContentInset.top;
            _totalHeight += (_praiseLabelHeight <= 0 ? _commentLabelHeight : _praiseLabelHeight) + _data.marginOnlyWordsPraiseInset.top + _data.marginOnlyWordsPraiseInset.bottom;
            break;
        }
        case SEGEnumOldPGCListViewType_DoublePic:
        case SEGEnumOldPGCListViewType_SingleSmallPic:
        {
            _contentLabelWidth = _data.widgetWidth - _data.marginSingleSmallPicPicturInset.left - _data.marginSingleSmallPicPicturInset.right - _data.SingleSmallPicPicturSize.width - _data.marginSingleSmallPicPicturInset.right;
            [self segcontentLabelWithContent:_data.pictorialData.pictorialTitle contentWidth:_contentLabelWidth  fontSize:16 textColor:kColor_1];
            
            _totalHeight = _data.SingleSmallPicPicturSize.height + _data.marginSingleSmallPicPicturInset.top + _data.marginSingleSmallPicPicturInset.bottom;
            break;
        }
        case SEGEnumOldPGCListViewType_SingleBigPic:
        {
            _contentLabelWidth = _data.widgetWidth - _data.marginSingleBigPicContentInset.left - _data.marginSingleBigPicContentInset.right;
            [self segcontentLabelWithContent:_data.pictorialData.pictorialTitle contentWidth:_contentLabelWidth  fontSize:16 textColor:kColor_1];
            
            _totalHeight += _contentLabelHeight + _data.marginSingleBigPicContentInset.top;
            _totalHeight += _data.SingleBigPicPicturSize.height + _data.marginSingleBigPicPictureInset.top;
            _totalHeight += (_praiseLabelHeight <= 0 ? _commentLabelHeight : _praiseLabelHeight) + _data.marginSingleBigPicPraiseInset.top + _data.marginSingleBigPicPraiseInset.bottom;
            break;
        }
//        case SEGEnumOldPGCListViewType_DoublePic:
//        {
//            break;
//        }
        case SEGEnumOldPGCListViewType_SmallTPic:
        {
            _contentLabelWidth = _data.widgetWidth - _data.marginSmallTPicContentInset.left - _data.marginSmallTPicContentInset.right;
            [self segcontentLabelWithContent:_data.pictorialData.pictorialTitle contentWidth:_contentLabelWidth  fontSize:16 textColor:kColor_1];
            
            _totalHeight += _contentLabelHeight + _data.marginSmallTPicContentInset.top;
            _totalHeight += _data.SmallTPicPicturSize.height + _data.marginSmallTPicPictureInset.top;
            _totalHeight += (_praiseLabelHeight <= 0 ? _commentLabelHeight : _praiseLabelHeight) + _data.marginSmallTPicPraiseInset.top + _data.marginSmallTPicPraiseInset.bottom;
            break;
        }
        default:
            break;
    }
}

//控件contentLabel文本布局计算
-(void)segcontentLabelWithContent:(NSString*)content contentWidth:(CGFloat)contentWidth fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    //modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(contentWidth, HUGE);
    container.linePositionModifier = modifier;
    container.truncationType = SEGTextTruncationTypeEnd;// "abcd…"
    container.maximumNumberOfRows =  2;
    
    _contentLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_contentLabelTextLayout)return;
    _contentLabelHeight = [modifier heightForLineCount:_contentLabelTextLayout.rowCount];
}

//控件praiseLabel文本布局计算
-(void)segpraiseLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    NSAttributedString *tagText = [self segAttachmentWithTitile:@"icon_zan" FontSize:13];
    if(tagText) [text insertAttributedString:tagText atIndex:0];
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    //modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth, HUGE);
    container.linePositionModifier = modifier;
    container.truncationType = SEGTextTruncationTypeEnd;// "abcd…"
    
    _praiseLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_praiseLabelTextLayout)return;
    _praiseLabelHeight = [modifier heightForLineCount:_praiseLabelTextLayout.rowCount];
}

//控件commentLabel文本布局计算
-(void)segcommentLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    NSAttributedString *tagText = [self segAttachmentWithTitile:@"icon_zan" FontSize:13];
    if(tagText) [text insertAttributedString:tagText atIndex:0];
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    //modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth, HUGE);
    container.linePositionModifier = modifier;
    container.truncationType = SEGTextTruncationTypeEnd;// "abcd…"
    
    _commentLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_commentLabelTextLayout)return;
    _commentLabelHeight = [modifier heightForLineCount:_commentLabelTextLayout.rowCount];
}

//控件timeDescLabel文本布局计算
-(void)segtimeDescLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    //modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth, HUGE);
    container.linePositionModifier = modifier;
    container.truncationType = SEGTextTruncationTypeEnd;// "abcd…"
    
    _timeDescLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_timeDescLabelTextLayout)return;
    _timeDescLabelHeight = [modifier heightForLineCount:_timeDescLabelTextLayout.rowCount];
}

- (NSAttributedString *)segAttachmentWithTitile:(NSString*)title FontSize:(CGFloat)fontSize{
    if (!title || title.length <= 0)return nil;
    
    UIImage *imageView = [UIImage imageWithContentsOfFile:SEGPicName(title)];
    
    CGFloat ascent = fontSize * 0.86;
    CGFloat descent = fontSize * 0.14;
    
    //(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0,0,0,4);
    
    SEGTextRunDelegate *delegate = [SEGTextRunDelegate new];
    delegate.ascent = ascent;
    delegate.descent = descent;
    delegate.width = imageView.size.width + 1;
    
    SEGTextAttachment *attachment = [[SEGTextAttachment alloc]init];
    attachment.content = imageView;
    attachment.contentInsets = contentInsets;
    
    NSMutableAttributedString *atr = [[NSMutableAttributedString alloc] initWithString:SEGTextAttachmentToken];
    [atr yy_setTextAttachment:attachment range:NSMakeRange(0, atr.length)];
    CTRunDelegateRef ctDelegate = delegate.CTRunDelegate;
    [atr yy_setRunDelegate:ctDelegate range:NSMakeRange(0, atr.length)];
    if (ctDelegate) CFRelease(ctDelegate);
    return atr;
}

@end

